package com.fsoft.core.aop;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fsoft.core.Global;
import com.fsoft.core.common.base.entity.BaseVo;
import com.fsoft.core.common.base.entity.UserVo;
import com.fsoft.core.context.FSContext;
import com.fsoft.core.utils.OgnlUtils;

/***
 * F-Soft 基础切面类
 * @package com.fsoft.core.aop
 * @author Fish
 * @email it.fish2010@foxmail.com
 * @date 2019-12-11
 * @CopyRight © F-Soft
 */
@SuppressWarnings("unchecked")
public class BaseAspect {

	private static Logger log = LoggerFactory.getLogger(BaseAspect.class);
	private long beginTime;
	private long endTime;

	private UserVo user;

	/****
	 * 重新组装参数： <BR>
	 * 1、如果是医科帐号，orgId默认为空，deptId参数不为空；<BR>
	 * 2、如果是供应商帐号，orgId默认有值，deptId参数为空；
	 * @author it_software(it.fish2010@foxmail.com)
	 * @date 2021-04-22
	 * @param joinPoint
	 * @throws Exception
	 */
	public void requestBind(JoinPoint joinPoint) throws Exception {
		beginTime = System.currentTimeMillis();
		user = FSContext.getInstance().getUser();
		Object[] vars = joinPoint.getArgs();
		for (Object obj : vars) {
			if (obj instanceof Map) {
				Map<String, Object> params = (Map<String, Object>) obj;
				// 参数重组: 对Batch关键字结束的查询key，进行转列表处理
				for (String key : params.keySet()) {
					if (key.endsWith("Batch") && params.get(key) instanceof String && OgnlUtils.isNotEmpty(params.get(key))) {
						String[] values = params.get(key).toString().split(",");
						params.put(key, values);
					}
				}
				// 设置登录帐号ID(userId)
				if (user != null && OgnlUtils.isEmpty(params.get(Global.F_USERID)))
					params.put(Global.F_USERID, user.getId());
				// 设置供应商ID(orgId)
				if (user != null && OgnlUtils.isEmpty(params.get(Global.F_ORGID)) && !StringUtils.equals(user.getOrgId(), Global.DEFAULT_ORG_ID))
					params.put(Global.F_ORGID, user.getOrgId());
			} else if (obj instanceof BaseVo) {
				BaseVo vo = (BaseVo) obj;
				if (user != null && OgnlUtils.isEmpty(vo.getCreateUserId()))
					vo.setCreateUserId(user.getId());
				if (user != null && OgnlUtils.isEmpty(vo.getOrgId()) && !StringUtils.equals(user.getOrgId(), Global.DEFAULT_ORG_ID))
					vo.setOrgId(user.getOrgId());
			}
		}
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
		response.addHeader("Access-Control-Allow-Origin", "*");
	}

	public void timeWatch(JoinPoint joinPoint) {
		endTime = System.currentTimeMillis();
		String className = joinPoint.getTarget().getClass().getSimpleName();
		String methodName = joinPoint.getSignature().getName();
		log.info(className + "." + methodName + "() 运行耗时：" + (endTime - beginTime) + " ms ");
	}
}
